package com.tablepet.activities.preview;

import android.app.PendingIntent;
import android.app.PictureInPictureParams;
import android.app.RemoteAction;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Icon;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.util.Rational;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.widget.LinearLayout;

import androidx.annotation.RequiresApi;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.tablepet.R;
import com.tablepet.activities.App;
import com.tablepet.databinding.ActivityPreviewBinding;

import java.util.ArrayList;
@RequiresApi(api = Build.VERSION_CODES.O)
public class PipActivity extends AndroidApplication {
    private static Render petholder;
    private ServiceBroadcastReceiver receiver;
    private ActivityPreviewBinding binding;
    @RequiresApi(api = Build.VERSION_CODES.O_MR1)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.touming);
        binding=ActivityPreviewBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        super.onCreate(savedInstanceState);
        initView();
        initReceiver();
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) binding.parentView.getLayoutParams();
        params.width =App.sp.getInt("screenWidth",0);
        params.height =App.sp.getInt("screenWidth",0);
        binding.parentView.setLayoutParams(params);
        enterPicInPic();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode()==KeyEvent.KEYCODE_BACK){
            enterPicInPic();
        }
        return true;
    }
    private void initReceiver() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("action1");
        intentFilter.addAction("action2");
        intentFilter.addAction("action3");
        receiver = new ServiceBroadcastReceiver();
        registerReceiver(receiver,intentFilter);
    }
    private void initView() {
        AndroidApplicationConfiguration configure=new AndroidApplicationConfiguration();
        configure.hideStatusBar=true;
        configure.r=configure.g=configure.b=configure.a=8;
        petholder=new Render();
        SurfaceView surfaceView = (SurfaceView) initializeForView(petholder, configure);
        SurfaceHolder holder = surfaceView.getHolder();
        holder.setFormat(PixelFormat.TRANSLUCENT);
        binding.parentView.addView(surfaceView);
    }
    public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) {
        super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
        if (!isInPictureInPictureMode){
            binding.parentView.setBackgroundColor(Color.alpha(255));
        }
        App.configChange = true;
    }

    private void enterPicInPic() {
        binding.background.setBackgroundColor(Color.alpha(0));
        binding.parentView.setBackgroundColor(Color.alpha(0));
        PictureInPictureParams.Builder builder = new PictureInPictureParams.Builder();
        builder.setSourceRectHint(new Rect(binding.parentView.getLeft(),binding.parentView.getTop(),binding.parentView.getRight(),binding.parentView.getBottom()));
        builder.setAspectRatio(new Rational(1,1));
        ArrayList<RemoteAction> actions = new ArrayList<>();
        actions.add(new RemoteAction(Icon.createWithResource(this, R.drawable.button1), "action1", "", getIntentToBroadcast("action1")));
        actions.add(new RemoteAction(Icon.createWithResource(this, R.drawable.button2), "action2", "", getIntentToBroadcast("action2")));
        actions.add(new RemoteAction(Icon.createWithResource(this, R.drawable.button3), "action3", "", getIntentToBroadcast("action3")));
        builder.setActions(actions);
        enterPictureInPictureMode(builder.build());
    }
    private PendingIntent getIntentToBroadcast(String name) {
        return  PendingIntent.getBroadcast(this, 1234, new Intent(name), 0);
    }
    static class ServiceBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (petholder.state==null)return;
            if (intent.getAction().equals("action1")){
                petholder.state.setAnimation(0, "activity/eat-bite", false);
            }
            if (intent.getAction().equals("action2")){
                petholder.state.setAnimation(0, "action/victory-pose-back-flip", false);
            }
            if (intent.getAction().equals("action3")){
                petholder.state.setAnimation(0, "activity/sleep", true);
            }
        }
    }
    @Override
    protected void onPause() {
        super.onResume();
    }
    @Override
    protected void onStop() {
        super.onStop();
        finish();
    }
    @Override
    protected void onPostResume() {
        super.onPostResume();
        binding.background.setBackgroundColor(Color.BLACK);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }
    @Override
    public boolean onPictureInPictureRequested() {
        return super.onPictureInPictureRequested();
    }
}